import 'package:flutter/material.dart';
import 'package:flutter_changdian_football/api/api_response.dart';
import 'package:flutter_changdian_football/api/req/court/req_court.dart';
import 'package:flutter_changdian_football/config/enum/load_state.dart';
import 'package:flutter_changdian_football/model/club/court_detail_model.dart';
import 'package:flutter_changdian_football/pages/home/home_controller.dart';
import 'package:flutter_changdian_football/utils/custom_toast.dart';
import 'package:flutter_changdian_football/widget/screenutil/custom_screenutil.dart';
import 'package:get/get_state_manager/get_state_manager.dart';

class LocalCourseDetailController extends GetxController {

  LocalCourseDetailController({
    required this.courtId
  });

  final String courtId;


  int currentBannerIndex = 0;
  changeBannerIndex(p) {
    currentBannerIndex = p;
    update();
  }

  ScrollController scrollController = ScrollController();
  // 范围 0-255
  int titleAlpha = 0;

  @override
  void onInit() {
    super.onInit();
    scrollController.addListener(() {
      if (scrollController.offset > 150.w) {
        titleAlpha = 255;
      } else if (scrollController.offset <= 0) {
        titleAlpha = 0;
      } else {
        titleAlpha = scrollController.offset * 255 ~/ 150.w;
      }
      update();
    });

    initData();
  }


  CourtDetailModel? courtDetailModel;

  LoadState state = LoadState.loading;

  initData() async {
    var resp = await ReqCourt.courtDetail(
      courtId: courtId,
      lat: '${HomeController.to.latitude ?? ''}',
      lng: '${HomeController.to.longitude ?? ''}'
    );
    if (resp.status == Status.completed) {
      if (resp.data['code'] == '200') {
        courtDetailModel = CourtDetailModel.fromJson(resp.data['data']);

        if (resp.data['data'] != null) {
          state = LoadState.success;
        } else {
          state = LoadState.empty;
        }

        update();
      } else {
        CustomToast.showTextToast('${resp.data['msg']}');
        state = LoadState.error;
        update();
      }
    } else {
      state = LoadState.network;
      update();
    }
  }



}